import java.util.ArrayList;


/**
 * Class than inherits from ArrayList that holds the cards
 * of the player. Overrides ArrayList find and contains methods
 * due to the nature of the Card class. Has special method removeCard
 * for removing cards from the hand
 * 
 * @author Malcheck s'Palcheck team
 * @param <T>
 */
public class Hand<T> extends ArrayList<T>
{
   /**
    * Finds an equivalent card to the card given and returns
    * the actual card sought
    * 
    * @param elementSought Card to find
    * @return the card, or null if not found
    */
   public Card find(Card elementSought)
   {
      boolean exists = false;
      int i = 0;
      Card current = null;
      
      // while we still have cards to look through and we haven't found
      // it yet, keep looking
      while(i < size() && !exists)
      {
         // set the current card we're looking at
         current = (Card) get(i);
         
         // if the suit and denomination are the same, we've found a match
         if(current.getSuit() == elementSought.getSuit() && 
            current.getDenomination() == elementSought.getDenomination())
         {
            exists = true;
         }
         
         i++;
      }
      
      return current;
   }
   
   
   /**
    * Returns true or false if there is an equivalent element in the hand
    * 
    * @param elementSought The element to find
    * @return True if it's in the hand, false if it's not
    */
   public boolean contains(Card elementSought)
   {
      Card find = find(elementSought);
      boolean exists = true;
      
      // ask the find method if it's in there. If find returns null,
      // then the card doesn't exist
      if(find == null)
      {
         exists = false;
      }
      
      return exists;
   }
   
   
   /**
    * Removes the equivalent card from the hand
    * 
    * @param elementToRemove The Card to remove
    * @return Boolean value indicating if the removal was successful
    */
   public boolean removeCard(Card elementToRemove)
   {
      boolean success = false;
      Card toRemove = find(elementToRemove);
      
      // ask the find method to get the equivalent element. 
      // If it's there, then call remove() to get rid of it
      if(toRemove != null)
      {
         remove(toRemove);
         success = true;
      }
      
      return success;
   }
   
   
}
